package cn.tqfeiyang.smartadmin.business.member.service;

import cn.tqfeiyang.smartadmin.business.member.entity.MemberLevelEntity;
import cn.tqfeiyang.smartadmin.business.member.mapper.MemberLevelMapper;
import cn.tqfeiyang.smartadmin.business.member.request.MemberLevelCreateDTO;
import cn.tqfeiyang.smartadmin.business.member.request.MemberLevelSearchDTO;
import cn.tqfeiyang.smartadmin.business.member.request.MemberLevelUpdateDTO;
import cn.tqfeiyang.smartadmin.business.member.response.MemberLevelDetailVO;
import cn.tqfeiyang.smartadmin.business.member.response.MemberLevelSearchVO;
import cn.tqfeiyang.smartadmin.commons.exception.BusinessException;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.toolbox.BeanUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.ObjectUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.StringUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.ValidationUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 会员等级服务类
 *
 * @author tqfeiyang
 * @since 2024-08-15
 */
@Service
public class MemberLevelService {

    /**
     * 会员等级映射接口
     */
    @Resource
    private MemberLevelMapper memberLevelMapper;

    /**
     * 查询会员等级
     *
     * @param dto 请求数据
     * @return 会员等级列表
     */
    public PageData<MemberLevelSearchVO> selectPage(MemberLevelSearchDTO dto) {
        ValidationUtils.validateProperties(dto);
        Page result = memberLevelMapper.selectPage(dto.getPageObject(), dto.getWrapper());
        return PageData.create(result, MemberLevelSearchVO.class);
    }

    /**
     * 获取会员等级
     *
     * @param id 会员等级id
     * @return 会员等级信息
     */
    public MemberLevelDetailVO selectDetail(Long id) {
        return BeanUtils.copy(checkEntityExists(id), MemberLevelDetailVO.class);
    }

    /**
     * 添加会员等级
     *
     * @param dto 请求数据
     * @return 会员等级id
     */
    public Long create(MemberLevelCreateDTO dto) {
        ValidationUtils.validateProperties(dto);

        LambdaQueryWrapper<MemberLevelEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberLevelEntity::getName, dto.getName());
        MemberLevelEntity existingEntity = memberLevelMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(existingEntity)) {
            throw new BusinessException("类型名称已经存在");
        }

        MemberLevelEntity insertedEntity = BeanUtils.copy(dto, MemberLevelEntity.class);
        insertedEntity.setCreateTime(LocalDateTime.now());
        insertedEntity.setUpdateTime(LocalDateTime.now());
        memberLevelMapper.insert(insertedEntity);
        return insertedEntity.getId();
    }

    /**
     * 修改会员等级
     *
     * @param id 会员等级id
     * @param dto 请求数据
     */
    public void update(Long id, MemberLevelUpdateDTO dto) {
        checkEntityExists(id);
        ValidationUtils.validateProperties(dto);

        if (StringUtils.isNotBlank(dto.getName())) {
            LambdaQueryWrapper<MemberLevelEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(MemberLevelEntity::getName, dto.getName())
                    .ne(MemberLevelEntity::getId, id);
            MemberLevelEntity existingEntity = memberLevelMapper.selectOne(wrapper);
            if (ObjectUtils.isNotEmpty(existingEntity)) {
                throw new BusinessException("类型名称已经存在");
            }
        }

        MemberLevelEntity updatedEntity = BeanUtils.copy(dto, MemberLevelEntity.class);
        updatedEntity.setId(id);
        updatedEntity.setUpdateTime(LocalDateTime.now());
        memberLevelMapper.updateById(updatedEntity);
    }

    /**
     * 删除会员等级
     *
     * @param id 会员等级id
     */
    public void delete(Long id) {
        checkEntityExists(id);
        memberLevelMapper.deleteById(id);
    }

    /**
     * 检查实体是否存在
     *
     * @param id 会员等级id
     */
    private MemberLevelEntity checkEntityExists(Long id) {
        MemberLevelEntity entity = memberLevelMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new BusinessException("会员等级不存在");
        }
        return entity;
    }
}
